package com.xyzh05.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.hibernate.annotations.Formula;

@Entity
@Table(name="vip_service")
@Searchable
@XmlRootElement
public class VipService extends BaseObject implements Serializable {
	private static final long serialVersionUID = 6586479596715334666L;
	
	public static final Short STATUS_BEGIN = 0;
	public static final Short STATUS_END = 1;
	public static final Short STATUS_DISABLED = 2;
	
	private Long id;
    private User editor;
    private User adviser;
    private User vip;
    private String vipServiceId;
    private Date startTime;
    private Date endTime;
    private Date createTime = new Date();
    private Date updateTime = new Date();
    private Short payment;
    private Short ispay;
    private Short status = 0;
    private String price;
    private String repay;
    private Set<Plan> plans = new HashSet<Plan>(0);
    private Set<Material> materials = new HashSet<Material>(0);   
    
    private String planStageName;
    private Date planStageDate;
    
    private List<VipServiceTransaction> vipServiceTransaction;
    
    @OneToMany( mappedBy = "vipService", fetch = FetchType.LAZY) 
    public List<VipServiceTransaction> getVipServiceTransaction() {
		return vipServiceTransaction;
	}

	public void setVipServiceTransaction(
			List<VipServiceTransaction> vipServiceTransaction) {
		this.vipServiceTransaction = vipServiceTransaction;
	}

	@Id @GeneratedValue(strategy=GenerationType.AUTO) @SearchableId    
    public Long getId() {
        return this.id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="editor_id", nullable=false)
    public User getEditor() {
		return editor;
	}

	public void setEditor(User editor) {
		this.editor = editor;
	}

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="adviser_id", nullable=false)
	public User getAdviser() {
		return adviser;
	}

	public void setAdviser(User adviser) {
		this.adviser = adviser;
	}

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="vip_id", nullable=false)
	public User getVip() {
		return vip;
	}

	public void setVip(User vip) {
		this.vip = vip;
	}
    
    @Column(name="vip_service_id", nullable=false, length=64)
    @SearchableProperty
    public String getVipServiceId() {
        return this.vipServiceId;
    }
    
    public void setVipServiceId(String vipServiceId) {
        this.vipServiceId = vipServiceId;
    }
    
    @Column(name="start_time", nullable=false)
    @SearchableProperty
    public Date getStartTime() {
        return this.startTime;
    }
    
    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }
    
    @Column(name="end_time", nullable=false)
    @SearchableProperty
    public Date getEndTime() {
        return this.endTime;
    }
    
    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }
    
    @Column(name="create_time", nullable=false)
    @SearchableProperty
    public Date getCreateTime() {
        return this.createTime;
    }
    
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    
    @Column(name="update_time", nullable=false)
    @SearchableProperty
    public Date getUpdateTime() {
        return this.updateTime;
    }
    
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
    
    @Column(name="payment", nullable=false)
    @SearchableProperty
    public Short getPayment() {
        return this.payment;
    }
    
    public void setPayment(Short payment) {
        this.payment = payment;
    }
    
    @Column(name="ispay", nullable=false)
    @SearchableProperty
    public Short getIspay() {
        return this.ispay;
    }
    
    public void setIspay(Short ispay) {
        this.ispay = ispay;
    }
    
    @Column(name="status", nullable=false)
    @SearchableProperty
    public Short getStatus() {
        return this.status;
    }
    
    public void setStatus(Short status) {
        this.status = status;
    }
    
    @Column(name="price", length=16)
    @SearchableProperty
    public String getPrice() {
        return this.price;
    }
    
    public void setPrice(String price) {
        this.price = price;
    }
    
    @Column(name="repay", length=16)
    @SearchableProperty
    public String getRepay() {
		return repay;
	}

	public void setRepay(String repay) {
		this.repay = repay;
	}

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="vipService")
    public Set<Plan> getPlans() {
        return this.plans;
    }
    
    public void setPlans(Set<Plan> plans) {
        this.plans = plans;
    }
    
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="vipService")
    public Set<Material> getMaterials() {
        return this.materials;
    }
    
    public void setMaterials(Set<Material> materials) {
        this.materials = materials;
    }
        
    @Formula("(select p.name from Plan p where p.service_id=id and p.status = 1)")
	public String getPlanStageName() {
		return planStageName;
	}

	public void setPlanStageName(String planStageName) {
		this.planStageName = planStageName;
	}

	@Formula("(select p.end_time from Plan p where p.service_id=id and p.status = 1)")
	public Date getPlanStageDate() {
		return planStageDate;
	}

	public void setPlanStageDate(Date planStageDate) {
		this.planStageDate = planStageDate;
	}

	public VipService(){		
	}
	
	public VipService(Long id, User editor, User adviser, User vip,
			String vipServiceId, Date startTime, Date endTime, Date createTime,
			Date updateTime, Short payment, Short ispay, Short status,
			String price, String repay, Set<Plan> plans, Set<Material> materials,
			String planStageName, Date planStageDate) {
		this.id = id;
		this.editor = editor;
		this.adviser = adviser;
		this.vip = vip;
		this.vipServiceId = vipServiceId;
		this.startTime = startTime;
		this.endTime = endTime;
		this.createTime = createTime;
		this.updateTime = updateTime;
		this.payment = payment;
		this.ispay = ispay;
		this.status = status;
		this.price = price;
		this.repay = repay;
		this.plans = plans;
		this.materials = materials;
		this.planStageName = planStageName;
		this.planStageDate = planStageDate;
	}

	public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        VipService pojo = (VipService) o;

        if (editor != null ? !editor.equals(pojo.editor) : pojo.editor != null) return false;
        if (adviser != null ? !adviser.equals(pojo.adviser) : pojo.adviser != null) return false;
        if (vip != null ? !vip.equals(pojo.vip) : pojo.vip != null) return false;
        if (vipServiceId != null ? !vipServiceId.equals(pojo.vipServiceId) : pojo.vipServiceId != null) return false;
        if (startTime != null ? !startTime.equals(pojo.startTime) : pojo.startTime != null) return false;
        if (endTime != null ? !endTime.equals(pojo.endTime) : pojo.endTime != null) return false;
        if (createTime != null ? !createTime.equals(pojo.createTime) : pojo.createTime != null) return false;
        if (updateTime != null ? !updateTime.equals(pojo.updateTime) : pojo.updateTime != null) return false;
        if (payment != null ? !payment.equals(pojo.payment) : pojo.payment != null) return false;
        if (ispay != null ? !ispay.equals(pojo.ispay) : pojo.ispay != null) return false;
        if (status != null ? !status.equals(pojo.status) : pojo.status != null) return false;
        if (price != null ? !price.equals(pojo.price) : pojo.price != null) return false;
        if (repay != null ? !repay.equals(pojo.repay) : pojo.repay != null) return false;

        return true;
    }

    public int hashCode() {
        int result = 0;
        result = (editor != null ? editor.hashCode() : 0);
        result = 31 * result + (adviser != null ? adviser.hashCode() : 0);
        result = 31 * result + (vip != null ? vip.hashCode() : 0);
        result = 31 * result + (vipServiceId != null ? vipServiceId.hashCode() : 0);
        result = 31 * result + (startTime != null ? startTime.hashCode() : 0);
        result = 31 * result + (endTime != null ? endTime.hashCode() : 0);
        result = 31 * result + (createTime != null ? createTime.hashCode() : 0);
        result = 31 * result + (updateTime != null ? updateTime.hashCode() : 0);
        result = 31 * result + (payment != null ? payment.hashCode() : 0);
        result = 31 * result + (ispay != null ? ispay.hashCode() : 0);
        result = 31 * result + (status != null ? status.hashCode() : 0);
        result = 31 * result + (price != null ? price.hashCode() : 0);
        result = 31 * result + (repay != null ? repay.hashCode() : 0);

        return result;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer(getClass().getSimpleName());

        sb.append(" [");
        sb.append("id").append("='").append(getId()).append("', ");
        sb.append("editor").append("='").append(getEditor()).append("', ");
        sb.append("adviser").append("='").append(getAdviser()).append("', ");
        sb.append("vip").append("='").append(getVip()).append("', ");
        sb.append("vipServiceId").append("='").append(getVipServiceId()).append("', ");
        sb.append("startTime").append("='").append(getStartTime()).append("', ");
        sb.append("endTime").append("='").append(getEndTime()).append("', ");
        sb.append("createTime").append("='").append(getCreateTime()).append("', ");
        sb.append("updateTime").append("='").append(getUpdateTime()).append("', ");
        sb.append("payment").append("='").append(getPayment()).append("', ");
        sb.append("ispay").append("='").append(getIspay()).append("', ");
        sb.append("status").append("='").append(getStatus()).append("', ");
        sb.append("price").append("='").append(getPrice()).append("', ");
        sb.append("repay").append("='").append(getRepay()).append("', ");
        sb.append("]");
      
        return sb.toString();
    }

}
